﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform;
using Sharp.ServiceContracts;
using Sharp.Web.Models;
using Microsoft.AspNet.Identity;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

#endregion

namespace Sharp.Web.Controllers
{
    [Authorize]
    [DirectController]
    public class MainController : Controller
    {
        // GET: Manage/Main
        public ActionResult Index()
        {
            var data = new IndexModel
            {
                UserName = User.Identity.Name,
                LeftTree = new Node()
                {
                    Text = ".",
                    NodeID = "Root",
                    Expanded = true,
                    AutoDataBind = false,
                    Checked = null
                }
            };

            var builder = new TreeBuilder<PermissionDTO>
            {
                ChangeData = (p, treeNode) =>
                {
                    treeNode.Text = p.Title;
                    treeNode.CustomAttributes.Add(new ConfigItem("route", p.Route.IsNullOrEmpty() ? "#" : p.Route));
                    treeNode.NodeID = p.Id.ToString();
                    treeNode.Expanded = true;
                }
            };

            IEnumerable<PermissionDTO> list = User.Identity.GetPermissions().Where(x => x.SystemCode == "Platform");

            builder.Build(list, data.LeftTree, null);


            return View(data);
        }

        public ActionResult Home()
        {
            return View();
        }
        public ActionResult Custom()
        {
            return new PartialViewResult();
        }
        public ActionResult About()
        {
            return new PartialViewResult();
        }



        #region DirectMethod

        [DirectMethod]
        [OutputCache(Duration = 86400, VaryByParam = "theme")]
        public DirectResult GetThemeUrl(string theme, bool reload)
        {
            Theme temp = (Theme)Enum.Parse(typeof(Theme), theme);

            Session["Ext.Net.Theme"] = temp;

            if (reload)
            {
                X.Reload();
                return this.Direct();
            }

            return this.Direct(MvcResourceManager.GetThemeUrl(temp));
        }

        [DirectMethod]
        [OutputCache(Duration = 86400, VaryByParam = "name")]
        public DirectResult GetHashCode(string name)
        {
            return this.Direct(Math.Abs(name.ToLower().GetHashCode()));
        }

        [DirectMethod]
        public ActionResult GetFormPanel(string id, string url)
        {
            var win = new Window
            {
                ID = id,
                Layout = "fit",
                IconCls = "#PageWhiteCode",
                Width = 925,
                Height = 650,
                Maximizable = true,
                Constrain = true,
                CloseAction = CloseAction.Destroy
            };
            win.Render("", RenderMode.AddTo);

            return this.Direct();
        }

        #endregion

    }
}